
##################################################################
# Time-consistent working party coding and aggregation by semester
##################################################################

# Programme:  cfsp-management02-semesterData.r
# Author:     Frank Haege, Department of Politics and Administration, University of Limerick
# Contact:		frank.haege@ul.ie

# Description
#############
# This script creates time-consistent titles and codes for working parties whose names changed over time
# or who were affected by organisational changes (e.g. splits, mergers, or subsumption into other groups).
# In the case of organisational changes, the working party always received the code and title of the larger entity
# (i.e., either the original working party before the split, or the new working party after the merger/subsumption).
# The script also aggregates the meeting duration data by semester, drops pre-2001 semesters, 
# and rescales the data to indicate the number of half-day meetings (rather than days).


# Remove all objects
rm(list = ls(all = TRUE))

# Load libraries
library(data.table)

# Set working directory
setwd("C:\\Users\\Frank\\Dropbox\\European Union\\CFSP attention\\EUP submission")

# Set options
options(stringsAsFactors=F)

# # Read data
load("Data management\\cfsp-management01-monthlyData.RData")


# Horizontal working parties
############################

# Recode horizontal working parties
names(table(df$wpname[df$area=="Horizontal"]))

# RELEX Working Party
df$wpname[df$id=="C37"] = "Working Party of Foreign Relations Counsellors"
df$id[df$id=="C37"] = "C01"

# Interim Political and Security Committee
df$wpname[df$id=="C38"] = "Political Committee/Political and Security Committee"
df$id[df$id=="C38"] = "A05"

# Ad hoc Working Party on the Security Model
df$wpname[df$id=="C18"] = "Working Party on OSCE and the Council of Europe"
df$id[df$id=="C18"] = "C20"

# Working Party on Planning
df$wpname[df$id=="C28"] = "Foreign Affairs Meeting"
df$nowp[df$id=="C28"] = "No"
df$id[df$id=="C28"] = "C00"

# Security Working Party
df$wpname[df$id=="C17"] = "Foreign Affairs Meeting"
df$nowp[df$id=="C17"] = "No"
df$id[df$id=="C17"] = "C00"

# Working Party on CFSP Administrative Affairs and Protocol
df$wpname[df$id=="C30"] = "Foreign Affairs Meeting"
df$nowp[df$id=="C30"] = "No"
df$id[df$id=="C30"] = "C00"

# Working Party on Fairs and Exhibitions
df$wpname[df$id=="C36"] = "Foreign Affairs Meeting"
df$nowp[df$id=="C36"] = "No"
df$id[df$id=="C36"] = "C00"

# Rename horizontal working parties
df$wpname[df$wpname=="United Nations Working Party"] = "United Nations"
df$wpname[df$wpname=="Political Committee/Political and Security Committee"] = "Political and Security Committee"
df$wpname[df$wpname=="Working Party of Foreign Relations Counsellors"] = "Foreign Relations Counsellors"
df$wpname[df$wpname=="Working Party on Consular Affairs"] = "Consular Affairs"
df$wpname[df$wpname=="Working Party on Conventional Arms Exports"] = "Conventional Arms Exports"
df$wpname[df$wpname=="Working Party on Global Disarmament and Arms Control"] = "Global Disarmament and Arms Control"
df$wpname[df$wpname=="Working Party on Human Rights"] = "Human Rights"
df$wpname[df$wpname=="Working Party on Non-Proliferation"] = "Non-Proliferation"
df$wpname[df$wpname=="Working Party on OSCE and the Council of Europe"] = "OSCE and the Council of Europe"
df$wpname[df$wpname=="Working Party on Public International Law"] = "Public International Law"
df$wpname[df$wpname=="Working Party on Terrorism (International Aspects)"] = "International Terrorism"
df$wpname[df$wpname=="Working Party on the Law of the Sea"] = "Law of the Sea"
df$wpname[df$wpname=="Working Party on Transatlantic Relations"] = "Transatlantic Relations"

# Generate indicator for coordinating groups
df$coord = 0
df$coord[df$id=="A05"] = 1
df$coord[df$id=="C01"] = 1
df$coord[df$id=="C46"] = 1
table(df$wpname, df$coord)


# Regional working parties
##########################

# Recode regional working parties
names(table(df$wpname[df$area=="Regional"]))

# Ad hoc Working Party on Joint EU-Africa Strategy
df$wpname[df$id=="C48"] = "Africa Working Party"
df$id[df$id=="C48"] = "C08"

# Working Party on South Africa
df$wpname[df$id=="C50"] = "Africa Working Party"
df$nowp[df$id=="C50"] = "Yes"
df$id[df$id=="C50"] = "C08"

# Working Party on Central Europe
df$wpname[df$id=="C12"] = "Working Party on Central and South East Europe"
df$id[df$id=="C12"] = "C45"

# Working Party on South East Europe
df$wpname[df$id=="C13"] = "Working Party on Central and South East Europe"
df$id[df$id=="C13"] = "C45"

# Working Party on Enlargement
df$wpname[df$id=="C52"] = "Working Party on Central and South East Europe"
df$id[df$id=="C52"] = "C45"

# Working Party on Enlargement and Countries Negotiating Accession to the EU
df$wpname[df$id=="C53"] = "Working Party on Central and South East Europe"
df$id[df$id=="C53"] = "C45"

# Rename regional working parties
df$wpname[df$wpname=="Africa Working Party"] = "Africa"
df$wpname[df$wpname=="Mashrak/Maghreb Working Party"] = "Mashrak/Maghreb"
df$wpname[df$wpname=="Asia-Oceania Working Party"] = "Asia-Oceania"
df$wpname[df$wpname=="Middle East/Gulf Working Party"] = "Middle East/Gulf"
df$wpname[df$wpname=="Working Party on Central and South East Europe"] = "Central and South East Europe"
df$wpname[df$wpname=="Working Party on Eastern Europe and Central Asia"] = "Eastern Europe and Central Asia"
df$wpname[df$wpname=="Working Party on South Africa"] = "South Africa"
df$wpname[df$wpname=="Working Party on the Western Balkans Region"] = "Western Balkans Region"
df$wpname[df$wpname=="Working Party on Central Europe"] = "Central Europe"
df$wpname[df$wpname=="Working Party on Latin America"] = "Latin America"
df$wpname[df$wpname=="Working Party on South East Europe"] = "South East Europe"
df$wpname[df$wpname=="Ad hoc Working Party on the Middle East Peace Process"] = "Middle East Peace Process"


# CSDP working parties
######################

# Recode CSDP working parties
names(table(df$wpname[df$area=="CSDP"]))

# Working Party on European Arms Policy
df$wpname[df$id=="C51"] = "Defence Meeting"
df$nowp[df$id=="C51"] = "No"

# Ad hoc Preparation Group on Defence Capabilities Development, Research, Acquisitions and Armaments
df$wpname[df$id=="C47"] = "Defence Meeting"
df$nowp[df$id=="C47"] = "No"
df$id[df$id=="C47"] = "C51"

# Rename CSDP working parties
df$wpname[df$wpname=="Committee for Civilian Aspects of Crisis Management"] = "Civilian Aspects of Crisis Management"
df$wpname[df$wpname=="Interim Military Committee/Military Committee (EUMC)"] = "Military Committee"
df$wpname[df$wpname=="Interim Military Working Party/Military Committee Working Group (EUMCWG)"] = "Military Committee Working Group"


# Development working parties
#############################

# Recode development working parties
names(table(df$wpname[df$area=="Development"]))

# Ad hoc Working Party on Harmonisation
df$wpname[df$id=="D04"] = "Development Meeting"
df$nowp[df$id=="D04"] = "No"
df$id[df$id=="D04"] = "D00"

# Working Party on Food aid
df$wpname[df$id=="D02"] = "Working Party on Humanitarian Aid and Food Aid"
df$id[df$id=="D02"] = "D05"

# Rename development working parties
df$wpname[df$wpname=="Working Party on Humanitarian Aid and Food Aid"] = "Humanitarian Aid and Food Aid"
df$wpname[df$wpname=="Working Party on Development Cooperation"] = "Development Cooperation"
df$wpname[df$wpname=="Working Party on Preparation for International Development Conferences"] = "Preparation for International Development Conferences"


# Trade working parties
#######################

# Recode trade working parties
names(table(df$wpname[df$area=="Trade"]))

# ACP/FIN Working Party
df$wpname[df$id=="C07"] = "ACP Working Party"
df$id[df$id=="C07"] = "C06"

# Ad hoc Post-Lom� Working Party
df$wpname[df$id=="C05"] = "ACP Working Party"
df$id[df$id=="C05"] = "C06"

# Rename trade working parties
df$wpname[df$wpname=="ACP Working Party"] = "ACP"
df$wpname[df$wpname=="Working Party on Commodities"] = "Commodities"
df$wpname[df$wpname=="Working Party on Dual-Use Goods"] = "Dual-Use Goods"
df$wpname[df$wpname=="Working Party on Trade Questions"] = "Trade Questions"
df$wpname[df$wpname=="Working Party on the Generalised System of Preferences"] = "Generalised System of Preferences"
df$wpname[df$wpname=="Working Party on EFTA"] = "EFTA"
df$wpname[df$wpname=="Article 133 Committee"] = "Article 133"


# Collapse merged working parties
df = data.table(df)
df = df[, .(dur=sum(dur)), by=setdiff(names(df), c("dur"))]
df = data.frame(df)

# Generate competence indicator
df$comp = NA
df$comp[df$area=="Horizontal"] = "CFSP"
df$comp[df$area=="Regional"] = "CFSP"
df$comp[df$area=="CSDP"] = "CFSP"
df$comp[df$area=="Development"] = "Ext. Rel."
df$comp[df$area=="Trade"] = "Ext. Rel."
table(df$comp, exclude=NULL)


# Aggregate data by semesters and create semester-to-semester change variables
##############################################################################

# Collapse by semester
df = data.table(subset(df, select=-c(datem, month, presmonth, lisbon, time)))
df = df[, .(dur=sum(dur)), by=setdiff(names(df), c("dur"))]

# Create semester time variable
df = df[, time:=seq_len(.N), by=c("id")]
# Create difference in duration variable
df = df[, diff.a:=abs(c(NA, diff(dur))), by=c("id")]
# Create lagged duration variable
df = df[, dur.lag:=shift(dur), by=c("id")]
df = data.frame(df)

# Drop first semester
df = df[df$yearsem!="1995h1", ]
# Create percentage change variable
# Half a meeting day added to avoid infinity results where lagged value is zero#
df$diff.r = (df$diff.a)/(df$dur.lag)*100
df$diff.r[df$diff.a==0] = 0
df$diff.r[df$diff.r==Inf] = (df$diff.a[df$diff.r==Inf])/(df$dur.lag[df$diff.r==Inf]+0.5)*100


# Drop years before CSDP structures came into operation
df = df[df$year>2000, ]
df$time = df$time-12

# Rescale meeting day difference to half-day meeting day difference
summary(df$dur)
df$dur = df$dur*2
# Rescale meeting day difference to half-day meeting day difference
df$diff.a = df$diff.a*2


# Save data
save(df, file="Data management\\cfsp-management02-semesterData.RData")